package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Medicinal;
import com.his.service.MedicinalService;
import com.his.utils.MyUtil;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Medicinal)表控制层
 *
 * @author makejava
 * @since 2020-10-24 11:11:27
 */
@RestController
@RequestMapping("medicinal")
public class MedicinalController {
    /**
     * 服务对象
     */
    @Resource
    private MedicinalService medicinalService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Medicinal selectOne(Integer id) {
        return this.medicinalService.queryById(id);
    }

    /**
     * 增加药品
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @PostMapping("insertOne")
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token,
                         @RequestBody @Validated({Const.Insert.class}) Medicinal medicinal) {
        try {
            medicinal.setCreateId(MyUtil.getUserByToken(token));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        Medicinal insert = medicinalService.insert(medicinal);
//        if (StringUtils.isEmpty(insert)){
//            return Rsp.fail().msg("药品名重复,添加失败");
//        }
        return Rsp.success(insert).msg("添加成功");
    }

    /*
     *删除药品
     */
    @DeleteMapping("deleteOne")
    public Rsp deleteByIdLogic(@RequestBody Medicinal medicinal) {
        if (medicinalService.deleteByIdLogic(medicinal.getId())) {
            return Rsp.success(null).msg("删除成功");
        } else {
            return Rsp.fail().msg("当前药品不存在,删除失败");
        }
    }

    /**
     * 修改药品
     */
    @PutMapping("updateOne")
    public Rsp updateOne(@RequestBody Medicinal medicinal) {
        Medicinal update = medicinalService.update(medicinal);
        if (StringUtils.isEmpty(update)) {
            return Rsp.fail().msg("药品名重复,修改失败");
        }
        return Rsp.success(update).msg("修改成功");
    }

    /**
     * 查询类别列表
     */
    @PostMapping("searchSomeMedicinalPage")
    public Rsp searchSomeMedicinalPage(@RequestBody PageParam<Medicinal> pageParam) {
        System.out.println(pageParam.toString());
        return Rsp.success(medicinalService.queryAll(pageParam));
    }

    /**
     * 上架药品
     */
    @PutMapping("shelfMedicinal")
    public Rsp shelfMedicinal(@RequestBody Medicinal medicinal) {
        System.out.println(medicinal);
        Integer shelf = medicinalService.shelfMedicinal(medicinal);
        if (shelf == 0) {
            return Rsp.fail().msg("修改失败");
        }
        return Rsp.success(null).msg("修改成功");
    }

    /**
     * 根据类别ID查询药品
     */
    @GetMapping("selectMedicinalBySuperId")
    public Rsp selectMedicinalBySuperId(Integer id) {
        return medicinalService.selectMedicinalBySuperId(id);

    }
}
